package com.yinhai.message.web.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yinhai.core.app.ta3.web.annotation.TaVO;
import com.yinhai.core.app.ta3.web.controller.BaseController;
import com.yinhai.core.common.api.base.IPage;
import com.yinhai.core.common.api.config.impl.SysConfig;
import com.yinhai.core.common.api.exception.AppException;
import com.yinhai.core.common.api.util.ValidateUtil;
import com.yinhai.core.common.ta3.dto.TaParamDto;
import com.yinhai.message.manage.wxregister.bpo.IWxConfigBpo;
import com.yinhai.message.manage.wxregister.vo.WxConfigVo;
import com.yinhai.message.web.bpo.IMessageWebBpo;
import com.yinhai.message.web.vo.MessageWebQueryVo;
import com.yinhai.message.web.vo.MessageWebUserVo;
import com.yinhai.modules.org.api.bpo.IOrgBpo;
import com.yinhai.modules.org.api.util.IOrgConstants;
import com.yinhai.project.exception.ExceptionController;

/**
 * Created by xiepx on 2017/3/28.
 */
@RequestMapping("messageWeb")
@Controller
public class MessageWebController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(ExceptionController.class);

    @Resource
    private IOrgBpo orgBpo;
    @Resource
    private IMessageWebBpo messageWebBpo;

    @Resource
	private IWxConfigBpo wxConfigBpo;
    
    @RequestMapping("WebController.do")
    public String execute(HttpServletRequest request, HttpServletResponse response){
    	LOG.debug("WebController - web=========================");
        String curSys = request.getParameter("curSys");
        String curUserId = request.getParameter("curUserid");
        String curName = request.getParameter("curName");
        String curOrgId = request.getParameter("curOrgid");
        String curPositionId = request.getParameter("curPositionid");

        List<WxConfigVo> list = wxConfigBpo.queryWxList(null, null);
        setData("curSys",curSys);
        setData("curUserId",curUserId);
        setData("curName",curName);
        setData("curOrgId",curOrgId);
        setData("curPositionId",curPositionId);
        setData("wxaccount",list);
        return "message/messageWeb";
    }
    @RequestMapping("WebControllerLocal.do")
    public String execute(){
    	LOG.debug("WebController - local========================");
        TaParamDto dto = getTaDto();
        String curSys = SysConfig.getSysConfig("curSyspathId");
        String curUserId = dto.getUser().getUserId();
        String curName = dto.getUser().getName();
        String curOrgId = dto.getUser().getOrgId();
        String curPositionId = String.valueOf(dto.getUser().getNowPosition().getPositionid());
        
        List<WxConfigVo> list = wxConfigBpo.queryWxList(null, null);
        setData("curSys",curSys);
        setData("curUserId",curUserId);
        setData("curName",curName);
        setData("curOrgId",curOrgId);
        setData("curPositionId",curPositionId);
        setData("wxaccount",list);
        return "message/messageWeb";
    }
    @RequestMapping("WebController!queryMessageUser.do")
    public String queryMessageUser(@TaVO MessageWebQueryVo msgWebVo) throws AppException{
        TaParamDto dto = getTaDto();

        msgWebVo.setGridId("userGd");
        msgWebVo.setStart(dto.getStart("userGd"));
        msgWebVo.setLimit(dto.getLimit("userGd"));
        msgWebVo.setEffective(IOrgConstants.EFFECTIVE_YES);
        IPage<MessageWebUserVo> messageUsers = messageWebBpo.queryMessageUser(msgWebVo);
        setList("userGd",messageUsers);
        return JSON;
    }

    /**
     * 异步查询组织
     *
     * @return
     * @throws com.yinhai.core.common.api.exception.AppException
     * @throws IOException
     */
    @RequestMapping(value = "WebController!webQueryAsyncOrgTree.do")
    public void webQueryAsyncOrgTree(HttpServletRequest request, HttpServletResponse response)
            throws com.yinhai.core.common.api.exception.AppException, IOException {
        String obj = request.getParameter("orgid");
        Long orgid = null;
        if (ValidateUtil.isNotEmpty(obj)) {
            orgid = Long.valueOf(obj);
        }
/*      String obj = request.getParameter("curOrgid");
        String pobj = request.getParameter("curPositionId");
        Long orgid = null;
        if (ValidateUtil.isNotEmpty(obj)) {
            orgid = Long.valueOf(obj);
        }
        Long positionid = null;
        if(ValidateUtil.isNotEmpty(pobj)){
            positionid = Long.valueOf(pobj);
        }*/
//        String treeJson = orgBpo.webQueryAsyncOrgTree(orgid, IOrgConstants.ADMIN_POSITIONID);
        String treeJson = orgBpo.webQueryAsyncOrgTree(orgid,  1l);
        writeJsonToClient(treeJson);
    }

    @RequestMapping("WebController!sendMessage.do")
    public String sendMessage() throws AppException{
        /*TaParamDto dto = getTaDto();
        //发送方
        TMUser sendUser = new TMUser();
        sendUser.setUserId(dto.getAsString("curUserid"));
        sendUser.setName(dto.getAsString("curName"));
        sendUser.setAppId(dto.getAsString("curSys"));
        //消息
        TMMessage tmMessage = new TMMessage();
        tmMessage.setTilte(dto.getAsString("mgHead"));
        tmMessage.setContent(dto.getAsString("mgContent"));
        String[] messageType = dto.getAsStringArray("mgType[]");
        tmMessage.setMessageTypes(Arrays.asList(messageType));

        ITMService messageSendService = new TMHttpService();
        List<IDto> selected = (List<IDto>) getGridSelected("userGd");
        for (int i = 0; i < selected.size(); i++) {
            //接受方
            TMUser receiveUser = new TMUser();
            receiveUser.setAppId(selected.get(i).getAsString("app"));
            receiveUser.setUserId(selected.get(i).getAsString("userid"));
            receiveUser.setName(selected.get(i).getAsString("name"));
            receiveUser.setMsm(selected.get(i).getAsString("tell"));
            receiveUser.setEmail(selected.get(i).getAsString("email"));

            try{
                messageSendService.sendMessage(sendUser, receiveUser, tmMessage);
            }catch (Exception e){
                e.printStackTrace();
                setMessage("消息发送失败","error");
                return JSON;
            }

        }

        setMessage("消息已发送","success");
*/
        return JSON;
    }

}
